package com.youchuang.action.admin;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.youchuang.action.BaseAction;
import com.youchuang.bean.Manager;
import com.youchuang.common.Constant;
import com.youchuang.service.AdminService;

/**
 * 登陆验证
 * 
 * @author Administrator
 *
 */
@Namespace(value = "/admin")
@ParentPackage("shopping-default")
public class LoginAction extends BaseAction {
	private static Logger logger = Logger.getLogger(LoginAction.class.getSimpleName());
	private Manager admin;
	
	@Resource
	private AdminService adminService;

	public Manager getAdmin() {
		return admin;
	}

	public void setAdmin(Manager admin) {
		this.admin = admin;
	}

	@Action(value = "loginManager", results = {
			@Result(name = Constant.SUCCESS, location = "/WEB-INF/jsp/ad/admain.jsp"),
			@Result(name = Constant.LOGIN, location = "/common/adlogin.jsp"), })
	public String execute() throws Exception {
		logger.info("method is execute");
		if (admin == null) {
			return Constant.LOGIN;
		}
		Manager manager = adminService.findManagerByAccount(admin.getAccount());

		if (manager == null) {
			request.setAttribute(Constant.LOGIN_ERR, "帐号或密码错误");
			return Constant.LOGIN;
		}
		String currentPassword = manager.getPassword();
		if (StringUtils.isEmpty(currentPassword) || !currentPassword.equals(admin.getPassword())) {
			request.setAttribute(Constant.LOGIN_ERR, "帐号或密码错误");
			return Constant.LOGIN;
		}
		session.put(Constant.A_USER, manager);
		return Constant.SUCCESS;
	}

}
